package com.financing.wap.web.controller.api;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.CommonConstantUtil.CommonStatus;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.DstUserClientTagInfoDTO;
import com.financing.wap.dto.DstUserClientViewDTO;
import com.financing.wap.dto.DstUserInfoDTO;
import com.financing.wap.service.ICommonDstService;
import com.financing.wap.service.IDstUserClientTagInfoService;
import com.financing.wap.service.IDstUserClientViewService;
import com.financing.wap.service.IDstUserInfoService;
import com.financing.wap.util.DateUtil;
import com.financing.wap.util.StringUtil;
import com.financing.wap.vo.DstUserTagListVO;
import com.financing.wap.web.controller.BaseController;

@Controller("dstUserInfoController")
@RequestMapping("/api/dstUserInfo")
public class DstUserInfoController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IDstUserInfoService dstUserInfoService;
	
	@Autowired
	private ICommonDstService commonDstService;
	
	@Autowired
	private IDstUserClientTagInfoService dstUserClientTagInfoService;
	
	@Autowired
	private IDstUserClientViewService dstUserClientViewService;
	
	/***
	 * 用户邀请列表
	 * @param request
	 * @param dstUserInfo
	 * @param pager
	 * @return
	 */
	@RequestMapping("/userInvitationList")
	@ResponseBody
	public ResultMap userInvitationList(HttpServletRequest request, DstUserInfoDTO dstUserInfo){
		ResultMap result = new ResultMap();
		try{
			logger.info("query userInvitationList list begin .......");
			String openid = request.getParameter("openid");
			if(StringUtil.isNullOrEmptyStr(openid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			result.setAttribute("list", commonDstService.userInvitationList(openid));
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			logger.info("query userInvitationList list end .......");
		}catch(Exception e){
			log.error("query userInvitationList error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
	
	/***
	 * 客户详情
	 * @param request
	 * @param dstUserInfo
	 * @param pager
	 * @return
	 */
	@RequestMapping("/clientView")
	@ResponseBody
	public ResultMap clientView(HttpServletRequest request){
		ResultMap result = new ResultMap();
		try{
			logger.info("query clientView begin .......");
			String openid = request.getParameter("openid");
			String clientOpenid = request.getParameter("clientOpenid");
			if(StringUtil.isNullOrEmptyStr(openid,clientOpenid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			//客户基本信息
			DstUserInfoDTO dstUserInfo = new DstUserInfoDTO();
			dstUserInfo.setUserWechatOpenid(clientOpenid);
			dstUserInfo.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			DstUserInfoDTO userinfo = dstUserInfoService
					.queryListGetFirst(dstUserInfo);
			//标记标签列表
			DstUserClientTagInfoDTO dstUserClientTagInfo=new DstUserClientTagInfoDTO();
			dstUserClientTagInfo.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			dstUserClientTagInfo.setUserClientOpenid(clientOpenid);
			dstUserClientTagInfo.setUserOpenid(openid);
			List<DstUserClientTagInfoDTO> tagList = dstUserClientTagInfoService.queryList(dstUserClientTagInfo);
			
			//访问分享总时长统计
			DstUserTagListVO vo=new DstUserTagListVO();
			vo.setClientOpenid(clientOpenid);
			vo.setUserOpenid(openid);
			Map<String, Object> visitCount = commonDstService.visitCountByClientOpenid(vo);
			String visitDate = visitCount.get("visitDate")==null?"0":(""+visitCount.get("visitDate"));
			visitCount.put("visitDateFormat", DateUtil.formatDateTime(Long.parseLong(visitDate)));
			result.setAttribute("visitCount",visitCount);
			
			//查询是否为客户
			DstUserClientViewDTO dstUserClientView=new DstUserClientViewDTO();
			dstUserClientView.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			dstUserClientView.setUserOpenid(openid);
			dstUserClientView.setUserClientOpenid(clientOpenid);
			DstUserClientViewDTO dview = dstUserClientViewService.queryListGetFirst(dstUserClientView);
			if(dview==null||dview.getId()==null){
				result.setAttribute("flagClient",false);
			}else{
				result.setAttribute("flagClient",true);
			}
			
			result.setAttribute("userClientName",dview.getUserClientName());
			result.setAttribute("tagList",tagList);
			result.setAttribute("userinfo",userinfo);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			logger.info("query clientView end .......");
		}catch(Exception e){
			log.error("clientView error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
	
	/***
	 * 更改客户信息
	 * @param request
	 * @param dstUserInfo
	 * @param pager
	 * @return
	 */
	@RequestMapping("/upClientInfo")
	@ResponseBody
	public ResultMap upClientInfo(HttpServletRequest request){
		ResultMap result = new ResultMap();
		try{
			logger.info("query clientView begin .......");
			String openid = request.getParameter("openid");
			String clientOpenid = request.getParameter("clientOpenid");
			String userClientName = request.getParameter("userClientName");
			if(StringUtil.isNullOrEmptyStr(openid,clientOpenid,userClientName)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			//查询是否为客户
			DstUserClientViewDTO dstUserClientView=new DstUserClientViewDTO();
			dstUserClientView.setDeleteTag(CommonStatus.DELETE_TAG_NORMAL);
			dstUserClientView.setUserOpenid(openid);
			dstUserClientView.setUserClientOpenid(clientOpenid);
			DstUserClientViewDTO dview = dstUserClientViewService.queryListGetFirst(dstUserClientView);
			if(dview!=null&&dview.getId()!=null){
				dview.setUserClientName(userClientName);
				dstUserClientViewService.update(dview);
			}
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			logger.info("query clientView end .......");
		}catch(Exception e){
			log.error("clientView error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		return result;
	}
}
